/*
 * Copyright (c) 2015-2020, www.dibo.ltd (service@dibo.ltd).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * https://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.dromara.common.mybatis.binding.parser;


import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
import net.sf.jsqlparser.expression.operators.conditional.XorExpression;
import net.sf.jsqlparser.expression.operators.relational.*;
import net.sf.jsqlparser.schema.Column;
import org.dromara.common.core.exception.InvalidUsageException;

import java.util.ArrayList;
import java.util.List;

/**
 * 关联注解条件解析器
 *
 * @author mazc@dibo.ltd
 * @version v2.0
 * @date 2019/3/30
 */
public class ConditionParser extends ExpressionVisitorAdapter {

    private List<Expression> expressList = new ArrayList<>(4);

    public ConditionParser() {
        this.expressList.clear();
    }

    /**
     * 添加错误信息
     *
     * @param errorMsg
     */
    private void throwError(String errorMsg) {
        throw new InvalidUsageException(errorMsg);
    }

    /**
     * 获取解析后的结果
     *
     * @return
     * @throws Exception
     */
    public List<Expression> getExpressList() {
        return expressList;
    }

    @Override
    public void visit(AndExpression andExpression) {
        andExpression.getLeftExpression().accept(this);
        andExpression.getRightExpression().accept(this);
    }

    // ----- 支持的条件
    @Override
    public void visit(EqualsTo equalsTo) {
        if (!(equalsTo.getLeftExpression() instanceof Column)) {
            throwError("= 条件左侧必须为字段/列名");
        }
        expressList.add(equalsTo);
    }

    @Override
    public void visit(GreaterThan greaterThan) {
        if (!(greaterThan.getLeftExpression() instanceof Column)) {
            throwError("> 条件左侧必须为字段/列名");
        }
        expressList.add(greaterThan);
    }

    @Override
    public void visit(NotEqualsTo notEqualsTo) {
        if (!(notEqualsTo.getLeftExpression() instanceof Column)) {
            throwError("!= 条件左侧必须为字段/列名");
        }
        expressList.add(notEqualsTo);

    }

    @Override
    public void visit(GreaterThanEquals greaterThanEquals) {
        if (!(greaterThanEquals.getLeftExpression() instanceof Column)) {
            throwError(">= 条件左侧必须为字段/列名");
        }
        expressList.add(greaterThanEquals);

    }

    @Override
    public void visit(InExpression inExpression) {
        if (!(inExpression.getLeftExpression() instanceof Column)) {
            throwError("IN 条件左侧必须为字段/列名");
        }
        expressList.add(inExpression);

    }

    @Override
    public void visit(MinorThan minorThan) {
        if (!(minorThan.getLeftExpression() instanceof Column)) {
            throwError("< 条件左侧必须为字段/列名");
        }
        expressList.add(minorThan);

    }

    @Override
    public void visit(MinorThanEquals minorThanEquals) {
        if (!(minorThanEquals.getLeftExpression() instanceof Column)) {
            throwError("<= 条件左侧必须为字段/列名");
        }
        expressList.add(minorThanEquals);

    }

    @Override
    public void visit(IsNullExpression isNullExpression) {
        if (!(isNullExpression.getLeftExpression() instanceof Column)) {
            throwError("Is Null 条件左侧必须为字段/列名");
        }
        expressList.add(isNullExpression);

    }

    @Override
    public void visit(LikeExpression likeExpression) {
        if (!(likeExpression.getLeftExpression() instanceof Column)) {
            throwError("Like 条件左侧必须为字段/列名");
        }
        expressList.add(likeExpression);

    }

    @Override
    public void visit(Between between) {
        if (!(between.getLeftExpression() instanceof Column)) {
            throwError("Between 条件左侧必须为字段/列名");
        }
        expressList.add(between);

    }

    //------- 暂不支持的条件
    @Override
    public void visit(OrExpression orExpression) {
        throwError("暂不支持 OR 连接条件: " + orExpression.toString());

    }

    @Override
    public void visit(XorExpression xorExpression) {
        throwError("暂不支持 XOR 关联条件: " + xorExpression.toString());

    }

}
